package ctm.javacode;

import java.io.* ;
import java.sql.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Date;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;


import org.apache.catalina.User;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

@WebServlet("/UserSuggest.do")

public class UserSuggest extends HttpServlet {
	private final String SUCCESS_VIEW = "UserSuggestDemo.jsp"; 
	private final String ERROR_VIEW = "commonError.jsp";  
	String nextpage = SUCCESS_VIEW ;
	String name = "";
	String sex = "";
	String work = "";
	
	double height = 0.0 ;
	double weight = 0.0 ;
	
	double calofday = 0.0 ;
	
	double prot = 0.0; // 蛋白質克數
	double fat = 0.0; // 脂肪克數
	double cho = 0.0; // 醣類克數
	double proportion_prot = 0.0; // 蛋白質比例
	double proportion_fat = 0.0;  // 脂肪比例
	double proportion_cho = 0.0;  // 醣類比例
	
	double quantity_Milk = 0.0;  // 牛奶份數
	double quantity_Veg = 0.0;   // 蔬菜份數
	double quantity_Fruit = 0.0; // 水果份數
	double quantity_Grain = 0.0; // 穀類份數
	double quantity_Meat = 0.0;  // 肉類份數
	double quantity_Fat = 0.0;// 油脂份數
	
	double breakfast_Milk = 0.0; //早餐奶類份數
	double lunch_Milk = 0.0; //午餐奶類份數
	double dinner_Milk = 0.0; //晚餐奶類份數
	
	double breakfast_Veg = 0.0; //早餐蔬菜類份數
	double lunch_Veg = 0.0; //午餐蔬菜類份數
	double dinner_Veg = 0.0; //晚餐蔬菜類份數
	
	double breakfast_Fruit = 0.0; //早餐水果類份數
	double lunch_Fruit = 0.0; //午餐水果類份數
	double dinner_Fruit = 0.0; //晚餐水果類份數
	
	double breakfast_Grain = 0.0; //早餐穀類份數
	double lunch_Grain = 0.0; //午餐穀類份數
	double dinner_Grain = 0.0; //晚餐穀類份數
	
	double breakfast_Meat = 0.0; //早餐肉類份數
	double lunch_Meat = 0.0; //午餐肉類份數
	double dinner_Meat = 0.0; //晚餐肉類份數
	
	double breakfast_Fat = 0.0; //早餐油脂份數
	double lunch_Fat = 0.0; //午餐油脂份數
	double dinner_Fat = 0.0; //晚餐油脂份數
	
	public boolean isTextNode(Node node){
		return node.getNodeName().equals("#text");
	}	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response )
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");	
		String user = request.getParameter("user");
		
		name = (String) request.getSession().getAttribute("login");

		work = request.getParameter( "work" ) ;	
		System.out.println(work);
		UserInfo(); // 取得使用者身高體重
		UserCal();	// 計算使用者每日攝取總熱量
			

		ThreeNutrients();
		SixType();   // 計算六大類的份數
		MealQuantity();
		
		double img_Grain = Adjust_quantity(quantity_Grain/4);
		double img_Fat = Adjust_quantity((quantity_Fat-1)/3);
		
/*		System.out.println("calofday:  " + calofday);
		System.out.println("奶: " + quantity_Milk);
		System.out.println("菜: " + quantity_Veg);
		System.out.println("果: " + quantity_Fruit);
		System.out.println("穀: " + quantity_Grain);
		System.out.println("肉: " + quantity_Meat);
		System.out.println("油: " + quantity_Fat);*/

		request.setAttribute("work", work);
		
		request.setAttribute("calofday", calofday);
		request.setAttribute("prot", prot);
		request.setAttribute("fat", fat);
		request.setAttribute("cho", cho);
		request.setAttribute("proportion_prot", proportion_prot) ; 
		request.setAttribute("proportion_fat", proportion_fat) ;    
		request.setAttribute("proportion_cho", proportion_cho) ;
		
		request.setAttribute("quantity_Milk", quantity_Milk) ; 
		request.setAttribute("quantity_Veg", quantity_Veg) ;    
		request.setAttribute("quantity_Fruit", quantity_Fruit) ;		
		request.setAttribute("quantity_Grain", quantity_Grain) ; 
		request.setAttribute("quantity_Meat", quantity_Meat) ;    
		request.setAttribute("quantity_Fat", quantity_Fat) ;		
		request.setAttribute("img_Grain", img_Grain) ;    
		request.setAttribute("img_Fat", img_Fat) ;
		
		request.setAttribute("breakfast_Milk", breakfast_Milk) ;	
		request.setAttribute("lunch_Milk", lunch_Milk) ;
		request.setAttribute("dinner_Milk", dinner_Milk) ;
		
		request.setAttribute("breakfast_Veg", breakfast_Veg) ;	
		request.setAttribute("lunch_Veg", lunch_Veg) ;
		request.setAttribute("dinner_Veg", dinner_Veg) ;
		
		request.setAttribute("breakfast_Fruit", breakfast_Fruit) ;	
		request.setAttribute("lunch_Fruit", lunch_Fruit) ;
		request.setAttribute("dinner_Fruit", dinner_Fruit) ;
		
		request.setAttribute("breakfast_Grain", breakfast_Grain) ;	
		request.setAttribute("lunch_Grain", lunch_Grain) ;
		request.setAttribute("dinner_Grain", dinner_Grain) ;
		
		request.setAttribute("breakfast_Meat", breakfast_Meat) ;	
		request.setAttribute("lunch_Meat", lunch_Meat) ;
		request.setAttribute("dinner_Meat", dinner_Meat) ;
		
		request.setAttribute("breakfast_Fat", breakfast_Fat) ;	
		request.setAttribute("lunch_Fat", lunch_Fat) ;
		request.setAttribute("dinner_Fat", dinner_Fat) ;
		request.getRequestDispatcher("userSuggestPrint.jsp").forward(request, response);
	}
	protected void doPost(HttpServletRequest request,
								HttpServletResponse response )
								throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");	
		String user = request.getParameter("user");
		
		name = (String) request.getSession().getAttribute("login");

		work = request.getParameter( "work" ) ;	
		System.out.println(work);
		UserInfo(); // 取得使用者身高體重
		UserCal();	// 計算使用者每日攝取總熱量
			

		ThreeNutrients();
		SixType();   // 計算六大類的份數
		MealQuantity();
		
		double img_Grain = Adjust_quantity(quantity_Grain/4);
		double img_Fat = Adjust_quantity((quantity_Fat-1)/3);
		
/*		System.out.println("calofday:  " + calofday);
		System.out.println("奶: " + quantity_Milk);
		System.out.println("菜: " + quantity_Veg);
		System.out.println("果: " + quantity_Fruit);
		System.out.println("穀: " + quantity_Grain);
		System.out.println("肉: " + quantity_Meat);
		System.out.println("油: " + quantity_Fat);*/

		request.setAttribute("work", work);
		
		request.setAttribute("calofday", calofday);
		request.setAttribute("prot", prot);
		request.setAttribute("fat", fat);
		request.setAttribute("cho", cho);
		request.setAttribute("proportion_prot", proportion_prot) ; 
		request.setAttribute("proportion_fat", proportion_fat) ;    
		request.setAttribute("proportion_cho", proportion_cho) ;
		
		request.setAttribute("quantity_Milk", quantity_Milk) ; 
		request.setAttribute("quantity_Veg", quantity_Veg) ;    
		request.setAttribute("quantity_Fruit", quantity_Fruit) ;		
		request.setAttribute("quantity_Grain", quantity_Grain) ; 
		request.setAttribute("quantity_Meat", quantity_Meat) ;    
		request.setAttribute("quantity_Fat", quantity_Fat) ;		
		request.setAttribute("img_Grain", img_Grain) ;    
		request.setAttribute("img_Fat", img_Fat) ;
		
		request.setAttribute("breakfast_Milk", breakfast_Milk) ;	
		request.setAttribute("lunch_Milk", lunch_Milk) ;
		request.setAttribute("dinner_Milk", dinner_Milk) ;
		
		request.setAttribute("breakfast_Veg", breakfast_Veg) ;	
		request.setAttribute("lunch_Veg", lunch_Veg) ;
		request.setAttribute("dinner_Veg", dinner_Veg) ;
		
		request.setAttribute("breakfast_Fruit", breakfast_Fruit) ;	
		request.setAttribute("lunch_Fruit", lunch_Fruit) ;
		request.setAttribute("dinner_Fruit", dinner_Fruit) ;
		
		request.setAttribute("breakfast_Grain", breakfast_Grain) ;	
		request.setAttribute("lunch_Grain", lunch_Grain) ;
		request.setAttribute("dinner_Grain", dinner_Grain) ;
		
		request.setAttribute("breakfast_Meat", breakfast_Meat) ;	
		request.setAttribute("lunch_Meat", lunch_Meat) ;
		request.setAttribute("dinner_Meat", dinner_Meat) ;
		
		request.setAttribute("breakfast_Fat", breakfast_Fat) ;	
		request.setAttribute("lunch_Fat", lunch_Fat) ;
		request.setAttribute("dinner_Fat", dinner_Fat) ;
		
/*		request.getSession().setAttribute("calofday", calofday);
		request.getSession().setAttribute("prot", prot);
		request.getSession().setAttribute("fat", fat);
		request.getSession().setAttribute("cho", cho);
		request.getSession().setAttribute("proportion_prot", proportion_prot) ; 
		request.getSession().setAttribute("proportion_fat", proportion_fat) ;    
		request.getSession().setAttribute("proportion_cho", proportion_cho) ;
		
		request.getSession().setAttribute("quantity_Milk", quantity_Milk) ; 
		request.getSession().setAttribute("quantity_Veg", quantity_Veg) ;    
		request.getSession().setAttribute("quantity_Fruit", quantity_Fruit) ;		
		request.getSession().setAttribute("quantity_Grain", quantity_Grain) ; 
		request.getSession().setAttribute("quantity_Meat", quantity_Meat) ;    
		request.getSession().setAttribute("quantity_Fat", quantity_Fat) ;		
		request.getSession().setAttribute("img_Grain", img_Grain) ;    
		request.getSession().setAttribute("img_Fat", img_Fat) ;
		
		request.getSession().setAttribute("breakfast_Milk", breakfast_Milk) ;	
		request.getSession().setAttribute("lunch_Milk", lunch_Milk) ;
		request.getSession().setAttribute("dinner_Milk", dinner_Milk) ;
		
		request.getSession().setAttribute("breakfast_Veg", breakfast_Veg) ;	
		request.getSession().setAttribute("lunch_Veg", lunch_Veg) ;
		request.getSession().setAttribute("dinner_Veg", dinner_Veg) ;
		
		request.getSession().setAttribute("breakfast_Fruit", breakfast_Fruit) ;	
		request.getSession().setAttribute("lunch_Fruit", lunch_Fruit) ;
		request.getSession().setAttribute("dinner_Fruit", dinner_Fruit) ;
		
		request.getSession().setAttribute("breakfast_Grain", breakfast_Grain) ;	
		request.getSession().setAttribute("lunch_Grain", lunch_Grain) ;
		request.getSession().setAttribute("dinner_Grain", dinner_Grain) ;
		
		request.getSession().setAttribute("breakfast_Meat", breakfast_Meat) ;	
		request.getSession().setAttribute("lunch_Meat", lunch_Meat) ;
		request.getSession().setAttribute("dinner_Meat", dinner_Meat) ;
		
		request.getSession().setAttribute("breakfast_Fat", breakfast_Fat) ;	
		request.getSession().setAttribute("lunch_Fat", lunch_Fat) ;
		request.getSession().setAttribute("dinner_Fat", dinner_Fat) ;*/
		
		request.getRequestDispatcher(nextpage).forward(request, response);

		
	} // end doGet()


	private void MealQuantity() {
		// 分配奶類餐次
		breakfast_Milk = Adjust_quantity(quantity_Milk *0.7);
		lunch_Milk = 0.0;
		dinner_Milk = quantity_Milk - breakfast_Milk; 
		// 分配蔬菜類餐次
		breakfast_Veg = Adjust_quantity(quantity_Veg * 0.2);
		lunch_Veg = Adjust_quantity((quantity_Veg - breakfast_Veg) / 2);
		dinner_Veg = quantity_Veg - breakfast_Veg - lunch_Veg; 
		// 分配水果類餐次
		breakfast_Fruit = Adjust_quantity(quantity_Fruit * 0.2);
		lunch_Fruit = Adjust_quantity((quantity_Fruit - breakfast_Fruit) / 2);
		dinner_Fruit = quantity_Fruit - breakfast_Fruit - lunch_Fruit; 
		// 分配全穀根莖類餐次
		breakfast_Grain = Adjust_quantity(quantity_Grain * 0.2);
		lunch_Grain = Adjust_quantity((quantity_Grain - breakfast_Grain) / 2);
		dinner_Grain = quantity_Grain - breakfast_Grain - lunch_Grain; 
		// 分配豆魚肉蛋類餐次
		breakfast_Meat = Adjust_quantity(quantity_Meat * 0.2);
		lunch_Meat = Adjust_quantity((quantity_Meat - breakfast_Meat) / 2);
		dinner_Meat = quantity_Meat - breakfast_Meat - lunch_Meat; 
		// 分配油脂及堅果種子類餐次
		breakfast_Fat = Adjust_quantity(quantity_Fat * 0.2);
		lunch_Fat = Adjust_quantity((quantity_Fat - breakfast_Fat) / 2);
		dinner_Fat = quantity_Fat - breakfast_Fat - lunch_Fat; 

	}

	private void SixType() {
		// TODO Auto-generated method stub
		if (calofday >= 2000) // 2000大卡以上奶類2份
			quantity_Milk = 2;
		else
			quantity_Milk = 1.5;
		
		
		if (sex.equals("male"))  // 男生蔬菜5份、水果4份
		{
			quantity_Veg = 5;
			quantity_Fruit = 4;
		}
		else  // female 女生蔬菜4份、水果3份
		{
			quantity_Veg = 4;
			quantity_Fruit = 3;
		}
		
		// 低脂奶類: prot 每份8g、fat每份4g、cho每份12g
		// 蔬菜:    prot 每份1g、fat每份0g、cho每份5g 
		// 水果:    prot 每份0g、fat每份0g、cho每份15g
		// 穀類:    prot 每份2g、fat每份0g、cho每份15g
	    // 豆魚肉蛋: prot 每份7g、fat每份5g、cho每份0g
	    // 油脂:    prot 每份0g、fat每份5g、cho每份0g
		
		quantity_Grain = (cho - quantity_Milk * 12 - quantity_Veg * 5 -
				          quantity_Fruit * 15) / 15; 
		quantity_Grain = Adjust_quantity(quantity_Grain);
		quantity_Meat = ( prot - quantity_Milk * 8 - quantity_Veg -
				          quantity_Grain * 2 ) / 7;
		quantity_Meat = Adjust_quantity(quantity_Meat);
		quantity_Fat = (fat - quantity_Milk * 4 - quantity_Meat * 5) / 5;
		quantity_Fat = Adjust_quantity(quantity_Fat);
	}

	private double Adjust_quantity(double quantity) {
		// TODO Auto-generated method stub
		DecimalFormat df = new DecimalFormat("#.#");
		quantity = Double.parseDouble(df.format(quantity));
		double decimal = quantity - (int)quantity;
		if (quantity < 0)
			return 0;
		else if (decimal <= 0.2)
			return (int)quantity;
		else if (decimal >= 0.8)
			return (int)(quantity+1);
		else
			return ((int)quantity)+0.5;
	}

	private void ThreeNutrients() {  // 蛋白質以1 g/kg計算，脂肪30%計算
		// TODO Auto-generated method stub
		prot = weight;  // 蛋白質 1 g/kg
		proportion_prot = prot * 4 / calofday;
		fat = calofday * 0.3 / 9;
		proportion_cho = 1 - 0.3 - proportion_prot;
		cho = calofday * proportion_cho / 4;	
		
		proportion_prot = proportion_prot * 100; // 蛋白質百分比
		proportion_fat = 30;  // 脂肪百分比
		proportion_cho = proportion_cho * 100;  // 醣類百分比
	}


	private void UserCal() {
		// TODO Auto-generated method stub
		double bmi = weight / ( height / 100 * height / 100 ) ;		
		if ( bmi < 18.5 ) // 過輕
		{
			if ( work.compareTo("little") == 0 )
				calofday = 35 * weight ; 
			else if ( work.compareTo("middle") == 0 )
				calofday = 40 * weight ;
			else
				calofday = 45 * weight ;
		} // end if
		else if ( bmi >= 18.5 && bmi <24 ) // 正常
		{
			if ( work.compareTo("little") == 0 )
				calofday = 30 * weight ; 
			else if ( work.compareTo("middle") == 0 )
				calofday = 35 * weight ;
			else
				calofday = 40 * weight ;
		} // end else if
		else if ( bmi >= 18.5 && bmi <=27 ) // 過重
		{
			if ( work.compareTo("little") == 0 )
				calofday = 25 * weight ; 
			else if ( work.compareTo("middle") == 0 )
				calofday = 30 * weight ;
			else
				calofday = 35 * weight ;
		} // end else if
		else //if ( bmi <=30 ) // 一度肥胖
		{
			if ( work.compareTo("little") == 0 )
				calofday = 20 * weight ; 
			else if ( work.compareTo("middle") == 0 )
				calofday = 30 * weight ;
			else if ( work.compareTo("large") == 0 )
				calofday = 35 * weight ;
		} // end else if
		
	}

	private void UserInfo() {
		// TODO Auto-generated method stub
		dbBean db = new dbBean() ;
		db.setDataSource("jdbc/ctmpllab");
		db.Connected();	
		
		try {
			ResultSet rs = db.ExcuteQuery("select weight, height, sex from accounttable " +
										  "where account='"	+ name + "'") ;
			rs.next();
			weight = Double.parseDouble(rs.getString("weight")) ;
			height = Double.parseDouble(rs.getString("height")) ;
			sex = rs.getString("sex");
		} catch (SQLException e) {
			e.printStackTrace();
			nextpage = ERROR_VIEW ;
		} // end try-catch
	}

}

